package Hash;

import java.nio.file.attribute.AclEntryType;
import java.util.*;

/**
 * @Author: Darven
 * @CreateTime: 2025-03-30  15:47
 * @Description: TODO
 */
public class P128 {
    public static void main(String[] args) {
        P128Demo p128Demo = new P128Demo();
        int[] arr = {100, 4, 200, 1, 3, 2};
        System.out.println(p128Demo.longestConsecutive2(arr));
    }
}

class P128Demo {
    public int longestConsecutive(int[] nums) {
        if(nums.length==0){
            return 0;
        }
        int ans = 1;
        Map<Integer, Integer> map = new HashMap<>();

        for (int n : nums) {
            map.put(n, n + 1);
        }
        int temp = 1;
        Set<Integer> flag=new HashSet<>();
        for (int num : map.keySet()) {
            if(flag.contains(num)){
                continue;
            }
            int i = map.get(num);

            while (map.containsKey(i)) {
                flag.add(i);
                i = map.get(i);
                temp++;
            }
            if (ans < temp) {
                ans = temp;
            }
            temp = 1;
        }

        return ans;

    }

    public int longestConsecutive2(int[] nums) {
        int ans = 0;
        Set<Integer> map=new HashSet<>();

        for (int n : nums) {
            if(map.contains(n))
                continue;
            map.add(n);
        }
        //重复元素使用set去掉
        for (int num : map) {
            if(map.contains(num-1)){
                continue;
            }
            int temp = 0;
            while (map.contains(num++)) {
                temp++;
            }
            ans=Math.max(temp, ans);

        }

        return ans;

    }
}
